package com.yxr.affairs.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yxr.affairs.AboutActivity;
import com.yxr.affairs.AddActivity;
import com.yxr.affairs.LoginActicity1;
import com.yxr.affairs.R;
import com.yxr.affairs.base.BaseFragment;
import com.yxr.affairs.utils.UserInfo1;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class AccountActivity1 extends BaseFragment {


    @BindView(R.id.top_view)
    View topView;
    @BindView(R.id.picture_img)
    ImageView pictureImg;
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.usename)
    TextView usename;
    @BindView(R.id.activity_username)
    RelativeLayout activityUsername;
    @BindView(R.id.activity_username1)
    RelativeLayout activityUsername1;

    @BindView(R.id.setting_exit_login)

    RelativeLayout settingExitLogin;
    Unbinder unbinder;
    private AccountActivity1 mAActivity;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_account;
    }

    @Override
    protected void initViews() {

        String versionName = getVersionName(mContext);
        version.setText("版本号  " + versionName);


        settingExitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("提示");
                builder.setMessage("是否确认退出");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                        dialog.dismiss();
                        //    _exit();
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        UserInfo1.exitLogin();


                        // getActivity().finish();

                        Intent intent = new Intent();
                        //SoilsenerActivity.class为想要跳转的Activity
                        intent.setClass(getActivity(), LoginActicity1.class);
                        startActivity(intent);


                    }
                });
                builder.create().show();
            }
        });

        activityUsername.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(mContext, AddActivity.class));

            }
        });

        if (!UserInfo1.isLogin()) {

            Intent intent = new Intent();
            //SoilsenerActivity.class为想要跳转的Activity
            intent.setClass(getActivity(), LoginActicity1.class);
            startActivity(intent);
        }

        activityUsername1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //SoilsenerActivity.class为想要跳转的Activity
                intent.setClass(getActivity(), AboutActivity.class);
                startActivity(intent);
            }
        });


    }

    public static String getVersionName(Context mContext) {
        PackageManager packageManager = mContext.getPackageManager();
        try {
            PackageInfo packInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
            String version = packInfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void updateViews(boolean isRefresh) {


    }


    @Override
    protected void onError_(Throwable e) {

    }

    @Override
    protected void onCancel_() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
